package com.hnevc.nbastar;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class NBAStarItemAdapter extends RecyclerView.Adapter<NBAStarItemAdapter.ItemViewHolder> {
    private Context context;
    private List<NBAStarItem> itemList;

    // 构造方法
    public NBAStarItemAdapter(Context context, List<NBAStarItem> itemList) {
        this.context = context;
        this.itemList = itemList;
    }

    // 填充子项布局
    @NonNull
    @Override
    public NBAStarItemAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nba_star, parent, false);
        return new ItemViewHolder(itemView);
    }

    // 创建内部静态类 ViewHolder，初始化布局
    static class ItemViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout llItem;
        public ImageView avatar;
        public TextView desc;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            llItem = itemView.findViewById(R.id.ll_item);
            avatar = itemView.findViewById(R.id.iv_avatar);
            desc = itemView.findViewById(R.id.tv_desc);
        }
    }

    // 绑定布局
    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, final int position) {
        // 点击跳转
        holder.llItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 从 v.getContext(), 跳转到 NBAStarDetailActivity.class
                Intent intent = new Intent(v.getContext(), NBAStarDetailActivity.class);
                intent.putExtra("item", itemList.get(position));
                v.getContext().startActivity(intent);
            }
        });

        NBAStarItem item = itemList.get(position);
        // 填充头像
        Glide.with(context)
                .load(item.getAvatar())
                .into(holder.avatar);

        // 填充描述
        String descStr = item.getClothNumber() + "号" + item.getName();
        holder.desc.setText(descStr);
    }

    // 返回子项数目
    @Override
    public int getItemCount() {
        return itemList.size();
    }
}
